﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Application.Permisos.ServiceAgent;
using System.Windows.Forms;

namespace Cesave.Presentation.Permisos.WinForms.Client.ViewModels.Permisos
{
	public partial class ExpedicionViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<Financiamiento> _financiamientos;

		/// <summary>
		/// 
		/// </summary>
		private Financiamiento _financiamiento;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<Financiamiento> Financiamientos
		{
			get
			{
				return _financiamientos;
			}
			private set
			{
				_financiamientos = value;
				if (_financiamientos != null)
					RaisePropertyChanged("Financiamientos");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Financiamiento Financiamiento
		{
			get
			{
				return _financiamiento;
			}
			private set
			{
				_financiamiento = value;
				if (_financiamiento != null)
					RaisePropertyChanged("Financiamiento");
			}
		}

		#endregion

		#region Private methods

		/// <summary>
		/// 
		/// </summary>
		private void LoadFinanciamientos()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					using (PermisosModuleServiceWrapper service = new PermisosModuleServiceWrapper())
					{
						e.Result = service.FindFinanciamientosByJuntaAndStatus(_pusSettings.Junta.JuntaGuid, true);
					}
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						Financiamientos = e.Result as List<Financiamiento>;
					else if (e.Error != null)
						ShowMessage(e.Error.Message);
				};
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentFinanciamiento(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if (bindingSource != null && bindingSource.Current != null)
				Financiamiento = bindingSource.Current as Financiamiento;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnFinanciamientosPropertyChanged()
		{
			_expedicionView.financiamientosBindingSource.DataSource = Financiamientos;
			_expedicionView.financiamientosBindingSource.CurrentChanged += SetCurrentFinanciamiento;
			SetCurrentFinanciamiento(_expedicionView.financiamientosBindingSource, new EventArgs());
		}

		private void OnFinanciamientoPropertyChanged()
		{
			// Actualizar los valores de Credito, FinanciamientoId y FinanciamientoGUID
			// del permiso actual.
		}

		#endregion


	}
}
